package com.doan.classcauhoi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.doan.gameshow.R;


import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.doan.classcauhoi.HienThiCacCauHoi;

public class MainActivity extends Activity {
	 
	public static List<Cauhoi> ds_cauhoi;
	RadioButton rdo_a,rdo_b,rdo_c,rdo_d;
	Cauhoi cauhientai;
	Button btnNext,btnPreVious,btnNopBai,btnhienthitatca;	
	TextView tv;
	ImageView img,imga,imgb,imgc,imgd;
	ProgressBar mProgressBar;
    CountDownTimer mCountDownTimer;
    CheckBox chkFlag;
    
    int socau=4;
	int index=0;
    int caudung=0;
    int i=0;
    int duyetqua=1;
    protected static final int TIMER_RUNTIME = 1000000; // in ms --> 10s
    protected boolean mbActive;
    public final static String EXTRA_MESSAGE = "com.doan.classcauhoi.Cauhoi";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ds_cauhoi=null;
		tv=(TextView)findViewById(R.id.txtView);
		rdo_a=(RadioButton)findViewById(R.id.rdo_a);
		rdo_b=(RadioButton)findViewById(R.id.rdo_b);
		rdo_c=(RadioButton)findViewById(R.id.rdo_c);
		rdo_d=(RadioButton)findViewById(R.id.rdo_d);
		btnNext=(Button)findViewById(R.id.btnNext);
		btnPreVious=(Button)findViewById(R.id.btnPrevious);
		btnNopBai=(Button)findViewById(R.id.btnNopBai);
		btnhienthitatca=(Button)findViewById(R.id.btnhienthi);
		img=(ImageView)findViewById(R.id.imgView);
		imga=(ImageView)findViewById(R.id.imgTraLoiA);
		imgb=(ImageView)findViewById(R.id.imgTraLoiB);
		imgc=(ImageView)findViewById(R.id.imgTraLoiC);
		imgd=(ImageView)findViewById(R.id.imgTraLoiD);
		chkFlag=(CheckBox)findViewById(R.id.chkFlag);
		
        Quanlycauhoi db=new Quanlycauhoi(this);
        try {
			db.createDataBase();				        
	    	ds_cauhoi=new ArrayList<Cauhoi>();
	        ds_cauhoi=(ArrayList<Cauhoi>) db.layNcaungaunhien(socau);
	        hienthi(index);
	        
	        btnNext.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					///xu ly phan tinh diem o day
					String cautraloi="";
					if(rdo_a.isChecked()==true)
						cautraloi="a";
					else if(rdo_b.isChecked()==true)
						cautraloi="b";
					else if(rdo_c.isChecked()==true)
						cautraloi="c";
					else if(rdo_d.isChecked()==true)
						cautraloi="d";
					else
						cautraloi="bo qua";
					
					Toast.makeText(getApplicationContext(), "Câu Trả Lời " + cautraloi, Toast.LENGTH_SHORT).show();
					if(cautraloi.equalsIgnoreCase(cauhientai.dapan))
					{
						caudung=caudung+1;
					}
					if(index<socau)
					{
						index++;
						duyetqua++;
					}
					if(index<socau)
						hienthi(index);
					
				}
			});  
	        
	        btnPreVious.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View argv) {
					// TODO Auto-generated method stub
					if(index>0)
					{
						index--;
					}
					if(index>=0)
						hienthi(index);
					
				}
	        	
	        });   
	        	        
	        
	        btnNopBai.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View argv) {
					// TODO Auto-generated method stub
					if(duyetqua<socau)
					{
						Toast.makeText(getApplicationContext(), "Bạn chưa làm " + (socau-index)+" câu còn lại", Toast.LENGTH_SHORT).show();
						
					}
					else						
					{
						///kich activity moi de bao ket qua
						Toast.makeText(getApplicationContext(), "Số câu đúng " + caudung, Toast.LENGTH_SHORT).show();
					}
				}
	        	
	        });   
	        
	        btnhienthitatca.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View argv) {
					// TODO Auto-generated method stub
					ArrayList<String> myList = new ArrayList<String>();
					//myList=ArrayList
					Intent intent = new Intent(MainActivity.this, HienThiCacCauHoi.class);				    
				    intent.putExtra(EXTRA_MESSAGE,  myList);
				    startActivity(intent);
				     
					/*ArrayList<Cauhoi> myList = new ArrayList<Cauhoi>();
					myList=(ArrayList<Cauhoi>)ds_cauhoi;
					//myList=ArrayList
					Intent intent = new Intent(MainActivity.this, HienThiCacCauHoi.class);				    
				    intent.putExtra("ds_cauhoi",  myList);
				    startActivity(intent);*/				
				}
	        	
	        });
	        
	        //Xử lý sự kiên với ProgressBar              
	        
	        mProgressBar = (ProgressBar)findViewById(R.id.pgBTime);

	        final Thread timerThread = new Thread() {
	            @Override
	            public void run() {
	                mbActive = true;
	                try {
	                    int waited = 0;
	                    while(mbActive && (waited < TIMER_RUNTIME)) {
	                        sleep(200);
	                        if(mbActive) {
	                            waited += 200;
	                            updateProgress(waited);
	                        }
	                    }
	                } catch(InterruptedException e) {
	                // do nothing
	                } finally {
	                onContinue();
	                }
	            }
	        };
	       timerThread.start(); 
	     
	        
	        
	        
	        
	            //Xử lý sự kiện check vào Flag
	        chkFlag.setOnClickListener(new View.OnClickListener() {
	                @Override
	                public void onClick(View v) {
	                       // TODO Auto-generated method stub
	                       boolean checked = ((CheckBox) v).isChecked();
	                       if (checked)
	                       {
	                    	   Cauhoi cauhoiedit=ds_cauhoi.get(index);
	                    	   cauhoiedit.flag=true;
	                    	   ds_cauhoi.set(index, cauhoiedit);
	                    	   chkFlag.setTag(cauhoiedit.get_id());
	                       }
	                       else
	                       {
	                    	   Cauhoi cauhoiedit=ds_cauhoi.get(index);
	                    	   cauhoiedit.flag=false;
	                    	   ds_cauhoi.set(index, cauhoiedit);
	                    	   chkFlag.setTag(cauhoiedit.get_id());
	                       }
	                }
	         });
	            
		
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this,"Lỗi kết nối \n Bạn nên khởi động lại chương trình", Toast.LENGTH_SHORT).show();
		}  

	}
	@Override
    public void onDestroy() {
        super.onDestroy();
    }
    public void updateProgress(final int timePassed) {
        if(null != mProgressBar) {
            // Ignore rounding error here
            final int progress = mProgressBar.getMax() * timePassed / TIMER_RUNTIME;
            mProgressBar.setProgress(progress);
        }
    }

    public void onContinue() {
      // perform any final actions here
    }

	public void hienthi(int vitri)
    {		
    	cauhientai=ds_cauhoi.get(vitri);
    	tv.setText(cauhientai.cauhoi);
    	if(cauhientai.image!=null)
    	{
    		img.setImageBitmap(BitmapFactory.decodeByteArray( cauhientai.image,0,cauhientai.image.length));
    	}else
    	{
    		img.setVisibility(View.GONE);
    	}
    	rdo_a.setText(cauhientai.cau_a);
    	rdo_b.setText(cauhientai.cau_b);
    	rdo_c.setText(cauhientai.cau_c);
    	rdo_d.setText(cauhientai.cau_d);
    	chkFlag.setChecked(cauhientai.flag);
    	
    	if(cauhientai.imagea!=null)
    	{
    		imga.setImageBitmap(BitmapFactory.decodeByteArray( cauhientai.imagea,0,cauhientai.imagea.length));
    	}else
    	{
    		imga.setVisibility(View.GONE);
    	}
    	if(cauhientai.imageb!=null)
    	{
    		imgb.setImageBitmap(BitmapFactory.decodeByteArray( cauhientai.imageb,0,cauhientai.imageb.length));
    	}else
    	{
    		imgb.setVisibility(View.GONE);
    	}
    	if(cauhientai.imagec!=null)
    	{
    		imgc.setImageBitmap(BitmapFactory.decodeByteArray( cauhientai.imagec,0,cauhientai.imagec.length));
    	}else
    	{
    		imgc.setVisibility(View.GONE);
    	}
    	if(cauhientai.imaged!=null)
    	{
    		imgd.setImageBitmap(BitmapFactory.decodeByteArray( cauhientai.imaged,0,cauhientai.imaged.length));
    	}else
    	{
    		imgd.setVisibility(View.GONE);
    	}
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
